const { addClass } = require('../modules/Teacher');
const teacherObj = require('../modules/Teacher');
const fs = require('fs');
const path = require('path');

let TeacherController = {
    get_index: function (req, res) {
        res.send('ok');
    },

    //教师登录验证
    post_login: async function (req, res) {
        let data = req.body;
        let resultDate = await teacherObj.checkLogin(data);
        res.send(resultDate);
    },

    //教师修改个人信息
    post_teacherMsg: async function (req, res) {
        let data = req.body;
        let resultDate = await teacherObj.updateMsg(data);
        res.send(resultDate);
    },

    //教师修改头像
    post_teacherImg: async function (req, res) {
        const files = req.files
        let is = fs.createReadStream(files.file.path);
        const filePath = path.resolve(__dirname, '../../../public/images/' + files.file.originalFilename);
        let os = fs.createWriteStream(filePath);
        is.pipe(os);
        is.on('end', function () {
            fs.unlinkSync(files.file.path);
        });
        let resultDate = {
            code: 200,
            msg: '上传文件成功',
            url: files.file.originalFilename,
        };
        res.send(resultDate);
    },

    //渲染班级
    get_myclass: async function (req, res) {
        let resultArr = {};
        console.log(req.query);
        await teacherObj.myclass(req.query.teacherid)
            .then(
                res => {
                    resultArr = res;
                }
            )
        res.send(resultArr);
    },

    //渲染所有学生
    get_allstudent: async function (req, res) {
        let resultArr = {};
        console.log(req.query.teachername);
        await teacherObj.allstudent(req.query.teachername)
            .then(
                res => {
                    resultArr = res;
                }
            )
            // console.log(resultArr);
        res.send(resultArr);
    },

    //渲染部分学生
    get_somestudent: async function(req, res) {
        let resultArr = {};
        await teacherObj.somestudent(req.query.classname)
            .then(
                res => {
                    resultArr = res;
                }
            )
            // console.log(resultArr);
        res.send(resultArr);
    },

    // //添加学生
    // post_addstudent: async function(req, res) {

    // },

    //删除学生
    post_delstudent: async function (req, res) {
        let userid = '';
        if (req.body.userid) {
            userid = req.body.userid;
        }
        // console.log(userid);
        await teacherObj.delstudent(userid);
        res.send({
            'code': 200,
            'msg': '删除成功'
        })
    },


    post_addClass: async function(req, res) {
        // console.log(req.body);
        if (req.body.textcontent == '' || req.body.textcontent == null) {
            return false;
        }
        let addClassResult = await teacherObj.addClass(req.body.classid, req.body.teacherid, req.body.textcontent);

        res.send(addClassResult)
    },

    //教课课程渲染
    post_techercourse: async function (req, res) {
        let resultArr = [];
        await teacherObj.techercourse(req.body.jobId)
            .then(
                res => {
                    resultArr = res;
                }
            )
        res.send(resultArr)
    },

    //删除课程
    post_deltechercourse: async function (req, res) {
        let resultArr = [];
        console.log("课程id", req.body.courseid);
        await teacherObj.deltechercourse(req.body.courseid)
            .then(
                res => {
                    resultArr = res;
                }
            )
        res.send(resultArr)
    },

    //添加课程
    post_addtechercourse: async function () {
        addtechercourse = await teacherObj.addtechercourse(
            req.body.ueseid,
            req.body.typeid,
            req.body.title,
            req.body.signup,
            req.body.img,
            req.body.type,
            req.body.teacherid,
        );
        res.send(addtechercourse)
    }
}
module.exports = TeacherController;